package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.ListedCompanyIndex;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.ABlockIndexService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;

/**
 * 上市公司指数管理
 *
 */

@Controller
@Api(value = "A股上市公司区块链指数")
@RequestMapping(value = ManagerVersion.API_V1_PREFIX + "listed/")
public class ABlockIndexController {

	@Autowired
	private ABlockIndexService aBlockIndexService;
	@Autowired
	private RedisServer redisServer;
	/**
	 * 交易所指数查询
	 */
	@ApiOperation(value = "A股上市公司区块链指数查询", notes = "A股上市公司区块链指数查询")
	@PostMapping("aBlockSel")
	@ResponseBody
	public Object exchangeSel(
							ListedCompanyIndex listedCompanyIndex,
							@RequestParam(value = "page") Integer pageIndex,
							@RequestParam(value = "limit") Integer pageSize,
							Map<String, Object> map) {
		try {
			listedCompanyIndex.setPageIndex(pageIndex);
			listedCompanyIndex.setPageSize(pageSize);
			PageInfo<ListedCompanyIndex> listd = aBlockIndexService.selectByListedList(listedCompanyIndex);
			ReturnResult<ListedCompanyIndex> result =
					new ReturnResult<ListedCompanyIndex>(0, "成功", listd.getList(), listd.getTotal());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/aBlockSel", "[back/auth/listed/aBlockSel error {}]", e);
			map.put("msg", "上市公司指数查询失败");
			return null;
		}
	}

	/**
	 * 交易所指数新增
	 */
	@ApiOperation(value = "A股上市公司区块链指数新增", notes = "A股上市公司区块链指数新增")
	@PostMapping("saveListedCompanyIndex")
	@ResponseBody
	public Object saveListedCompanyIndex(ListedCompanyIndex listedCompanyIndex) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"新增上市公司指数");
		try {
			ListedCompanyIndex index = aBlockIndexService.selectByEntity(listedCompanyIndex);
			if(null != index){
				return new ReturnResult<ListedCompanyIndex>(-1, "重复添加", null, 0);
			}
			listedCompanyIndex.setDeleteFlag(false);
			listedCompanyIndex.setCreateTime(new Date());
			int res = aBlockIndexService.insertSelective(listedCompanyIndex);
			return new ReturnResult<ListedCompanyIndex>(0, "新增成功", null, res);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/saveListedCompanyIndex",e.getMessage(), e);
			return new ReturnResult<ListedCompanyIndex>(-1, "新增失败", null, 0);
		}
	}

	/**
	 * 交易所指数修改
	 */

	@ApiOperation(value = "A股上市公司区块链指数修改", notes = "A股上市公司区块链指数修改")
	@PostMapping("updateListedCompanyIndex")
	@ResponseBody
	public Object updateListedCompanyIndex(ListedCompanyIndex listedCompanyIndex) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"修改上市公司指数");
		try {
			listedCompanyIndex.setUpdateTime(new Date());
			int res = aBlockIndexService.updateByPrimaryKey(listedCompanyIndex,aBlockIndexService.selectByPrimaryKey(listedCompanyIndex.getId()));
			return new ReturnResult<ListedCompanyIndex>(0, "保存成功", null, res);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/updateListedCompanyIndex",e.getMessage(), e);
			return new ReturnResult<ListedCompanyIndex>(-1, "保存失败", null, 0);
		}
	}

	@ApiOperation(value = "A股上市公司区块链指数上下架", notes = "A股上市公司区块链指数上下架")
	@PostMapping("aBlockenable")
	@ResponseBody
	public Object exchangeEnable(@RequestParam("id") Integer id, @RequestParam("enableFlag") String enableFlag,
			Map<String, Object> map) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"上市公司指数上下架");
		try {
			ListedCompanyIndex listedCompanyIndex = new ListedCompanyIndex();
			listedCompanyIndex.setUpdateTime(new Date());
			if (StringUtils.isNotEmpty(enableFlag)) {
				listedCompanyIndex.setEnableFlag("false".equals(enableFlag) ? true : false);
			}
			listedCompanyIndex.setId(id);
			int res = aBlockIndexService.updateByPrimaryKey(listedCompanyIndex,aBlockIndexService.selectByPrimaryKey(id));
			return new ReturnResult<ListedCompanyIndex>(0, "修改成功！", res, 0);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/aBlockenable", "[back/auth/listed/aBlockenable error {}]", e);
			map.put("msg", "上市公司指数上下架失败");
			return new ReturnResult<ListedCompanyIndex>(-1, "保存失败", null, 0);
		}
	}

	/**
	 * 交易所指数删除
	 */
	@ApiOperation(value = "A股上市公司区块链指数删除", notes = "A股上市公司区块链指数删除")
	@PostMapping("aBlockDel")
	@ResponseBody
	public Object exchangeUpdelete(@RequestParam("id") Integer id, Map<String, Object> map) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"上市公司指数删除");
		try {
			aBlockIndexService.deleteByPrimaryKey(id,aBlockIndexService.selectByPrimaryKey(id));
			return new ReturnResult<ListedCompanyIndex>(0, "删除成功！", 1, 0);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/aBlockDel", "[back/auth/listed/aBlockDel error {}]", e);
			map.put("msg", "上市公司指数删除失败");
			return new ReturnResult<ListedCompanyIndex>(-1, "删除失败", null, 0);
		}
	}

	/** 上市公司指数管理 */
	@GetMapping("listedManage")
	public String listedCompany() {
		return "/listedcompany/listedcompany";
	}

	/** 上市公司指数修改 */
	@GetMapping("listededit")
	public String listEdedit(@RequestParam("id") Integer id, Model model) {
		ListedCompanyIndex listedCompanyIndex = aBlockIndexService.selectByPrimaryKey(id);
		model.addAttribute("lcindex", listedCompanyIndex);
		return "/listedcompany/listededit";
	}

	/** 上市公司指数新增 */
	@GetMapping("listedcreate")
	public String listedCreate() {
		return "/listedcompany/listedcreate";
	}

}
